package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.CourseDao;
import com.zretc.support.entity.Course;
import com.zretc.support.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseDao courseDao;

    @Override
    public Page<Course> getCourseByPage(String keywords, int page, int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);

        return courseDao.getCourseByPage(keywords,  pageable);
    }

    @Override
    public Page<Course> getCourseByPageByTea(String keywords, int page, int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);

        return courseDao.getCourseByPageByTea(keywords,  pageable);
    }

    @Override
    public Page<Course> getCourseByTeacher(String userId, String keywords, int page, int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);

        return courseDao.getCourseByTeacher(userId,keywords,  pageable);
    }

    @Override
    public Page<Course> getCourseByStudent(String userId, String keywords, int page, int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);

        return courseDao.getCourseByStudent(userId,keywords,  pageable);
    }

    @Override
    public Course addCourse(Course course) {
        return courseDao.saveAndFlush(course);
    }

    @Override
    public Course updateCourse(Course course) {
        return courseDao.saveAndFlush(course);
    }

    @Override
    public String deleteCourse(Long courseId) {
        if(courseDao.getOne(courseId)==null){
            return "没有该课程";
        }else{
            courseDao.deleteById(courseId);
            return "删除成功";
        }
    }

    @Override
    public Course getCourseById(Long courseId) {
        System.out.println(courseDao.findById(courseId).get());
        return courseDao.findById(courseId).get();
    }

    @Override
    public List<Course> getAllCourse() {
        return courseDao.findAll();
    }

    @Override
    public List<Long> getCourseByCourseName(String courseName) {
        List<Course> courseList = courseDao.getCourseByCourseNameContaining(courseName);
        List<Long> courseIdList = new ArrayList<>();
        for (Course c:courseList) {
            courseIdList.add(c.getCourseId());
        }
        return courseIdList;
    }

    @Override
    public List<Course> getAllCourseByTea(String userId) {
        return courseDao.getAllCourseByTea(userId);
    }
}
